package com.ssyt.ec.common.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.ssyt.ec.stu.entity.UserEntity;

/**
 * 当前项目工具
 * 
 * @author Administrator
 *
 */
public class AppUtil {

	private static Logger LOG = Logger.getLogger(AppUtil.class);

	private AppUtil() throws IllegalAccessException {
		throw new IllegalAccessException();
	}

	/**
	 * 判断classes是否有登录
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isLogin(HttpServletRequest request) {
		if (request.getSession().getAttribute("classes") == null) {
			return false;
		}
		return true;
	}

	/**
	 * 获取放到Session里面的classes对象
	 * 
	 * @param request
	 * @return
	 */
	public static UserEntity getUser(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Object obj = request.getSession().getAttribute("classes");
			if (obj == null) {
				request.setAttribute("msg", "会话已经关闭！");
				request.getRequestDispatcher("/login.jsp").forward(request,
						response);
			} else {
				return (UserEntity) request.getSession()
						.getAttribute("classes");
			}
		} catch (ServletException | IOException e) {
			LOG.error(e.getMessage(), e);
		}
		System.out.println(getUser(request, response).getId());
		throw new RuntimeException("会话已经关闭！获取Session中的内容失败");
	}

	/**
	 * 获取放到Session里面的classes的id
	 * 
	 * @param request
	 * @return
	 */
	public static int getUserId(HttpServletRequest request,
			HttpServletResponse response) {
		return getUser(request, response).getId();
	}
}
